#define _CRT_SECURE_NO_WARNINGS 1
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 1e6 + 10;
int a[N];

void solve() {
	int n, k; cin >> n >> k;
	for (int i = 1; i <= n; i++) cin >> a[i];
	deque<int> dq;
	for (int i = 1; i <= n; i++) {
		while (dq.size() && dq.front() <= i - k) dq.pop_front();
		while (dq.size() && a[dq.back()] >= a[i]) dq.pop_back();
		dq.push_back(i);
		if (i >= k) cout << a[dq.front()] << " ";
	}
	cout << '\n';
	while (dq.size()) dq.pop_back();
	for (int i = 1; i <= n; i++) {
		while (dq.size() && dq.front() <= i - k) dq.pop_front();
		while (dq.size() && a[dq.back()] <= a[i]) dq.pop_back();
		dq.push_back(i);
		if (i >= k) cout << a[dq.front()] << " ";
	}
	cout << '\n';
}

int main() {
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	solve();
	return 0;
}